package com.zhangjie.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zhangjie.model.hosp.Department;
import com.zhangjie.repository.DepartmentRepository;
import com.zhangjie.service.DepartmentService;
import com.zhangjie.vo.hosp.DepartmentQueryVo;
import com.zhangjie.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    //上传科室保存
    @Override
    public void save(Map<String, Object> paramMap) {
        //传递过来的paramMap转换成对象
        String  paramMapString= JSONObject.toJSONString(paramMap);//先把他转换成字符串
        Department department = JSONObject.parseObject(paramMapString, Department.class);
        //查询mg数据库是否有该数据  根据医院编号和科室编号
        Department departmentExist=departmentRepository
                .getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        //判断
        if (departmentExist!=null){//如果mg里面有数据
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }else {//没有数据，进行添加
            department.setUpdateTime(new Date());
            department.setCreateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    //查找科室
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建Pageable对象，设置当前页，和每页记录数  page:0是第一页
        Pageable pageable=PageRequest.of(page-1,limit);
        //创建Example对象 定义查找规则
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//这是模糊查询
                .withIgnoreCase(true);//忽略大小写
        Example<Department> example = Example.of(department, matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override//删除医院接口
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null){
            //调用方法
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override//根据医院编号，查询医院所有科室列表 ->封装
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建一个list集合 //封装最后的数据
        List<DepartmentVo> resList=new ArrayList<>();
        //MongoDB根据医院编号，查询所有的科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);//查询
        //所有科室的列表信息
        List<Department> departmentList = departmentRepository.findAll(example);
        //根据大科室编号 bigcode 分组，获取每个大科室里面的下级小科室
        Map<String, List<Department>> departmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历departmentMap
        for (Map.Entry<String,List<Department>> entry:departmentMap.entrySet()){
            //得到的是大科室的编号
            String bigCode = entry.getKey();
            //得到大科室编号分组后的数据
            List<Department> departmentGroupList = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigCode);
            //得到科室集合里面的名称
            departmentVo1.setDepname(departmentGroupList.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> children=new ArrayList<>();
            for (Department department:departmentGroupList){//遍历分组后的集合
                DepartmentVo departmentVo2=new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());//封装科室编号
                departmentVo2.setDepname(department.getDepname());//封装科室名称
                children.add(departmentVo2);
            }
            //把小科室的list集合放到大科室里面
            departmentVo1.setChildren(children);
            //放到最终的resList里面
            resList.add(departmentVo1);
        }
        return resList;
    }

    @Override//根据医院，科室编号查询科室名字
    public String geDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }

}
